#pragma once

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdarg>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ctime>

// 日志级别
#define DEBUG   0
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

const char* gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

// 完整的日志功能，至少有：日志等级 时间 日志内容 支持用户自定义
void logMessage(int level, const char* format, ...)
{
    char stdBuffer[1024]; // 日志的标准部分
    time_t timestamp = time(nullptr); // 时间戳

    snprintf(stdBuffer, sizeof(stdBuffer), "[%s][%ld]", gLevelMap[level], timestamp);

    char logBuffer[1024]; // 自定义部分
    va_list args;
    va_start(args, format);
    vsnprintf(logBuffer, sizeof (logBuffer), format, args);
    va_end(args);

    // printf("%s%s\n", stdBuffer, logBuffer);
    FILE* fp = fopen("log.txt", "a");
    fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
    fclose(fp);
}